import axios from "axios"
import router from "../router"

axios.defaults.timeout = 3000
axios.defaults.withCredentials = true
//Content-Type 响应头
axios.defaults.headers.post["Content-Type"] =
  "application/x-www-form-urlencoded;charset=UTF-8"
// let token = `${localStorage.getItem('token')}`;
// if (token){
//     axios.defaults.headers.post['authorization'] = token;
//     axios.defaults.headers.get['authorization'] = token;
// }

//基础url
//axios.defaults.baseURL = "http://localhost:8080";
axios.defaults.baseURL = "http://101.201.70.102:8080"

axios.interceptors.request.use(
  (config) => {
    config.headers.token = `${localStorage.getItem("token")}`
    return config
  },
  (error) => Promise.reject(error)
)

//响应拦截器
axios.interceptors.response.use(
  (response) => {
    if (response.status === 200) {
      return Promise.resolve(response.data)
    } else {
      return Promise.resolve(response)
    }
  },
  (error) => {
    console.log(error.response)
  }
)

export function get(url, params = {}) {
  console.log(url)
  return new Promise((resolve, reject) => {
    axios
      .get(url, { params: params })
      .then((response) => {
        console.log(response)
        resolve(response.data||response)
      })
      .catch((error) => reject(error))
  })
}

export function post(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios
      .post(url, data)
      .then((response) => {
        resolve(response)
      })
      .catch((error) => reject(error))
      .then()
  })
}
